﻿@{
    ViewBag.Title = "客户管理";
    Layout = "~/Views/Shared/InfoMaste.cshtml";
}
<table id="dg" class="easyui-datagrid" style="width:100%; height: 600px;"
       url="/Customer/GetCustomerPageList" toolbar="#tb" title="客户信息" rownumbers="true" pagination="true"
       pagelist="[15]" fitcolumns="true" singleselect="true">
    <thead>
        <tr>
            <th field="CustomerCode" align="right" width="80">
                客户编号
            </th>
            <th field="CustomerName" align="right" width="80">
                客户姓名
            </th>
            <th field="CustomerLevelName" align="right" width="80">
                客户级别
            </th>
            <th field="Mobile" align="right" width="120">
                手机号码
            </th>
            <th field="Balance" align="right" width="80">
                余额
            </th>
            <th field="Tag" align="right" width="80">
                标签
            </th>
            <th field="Remark" align="right" width="80">
                备注
            </th>
            <th field="IsValid" align="right" width="80" formatter="formatter_IsValid">
                是否有效
            </th>
            <th field="CreateTime" align="right" width="80">
                创建时间
            </th>
        </tr>
    </thead>
</table>
<div id="tb" style="padding: 3px; height: auto;">
    <span>客户编号</span>
    <input id="CustomerCode" class="easyui-textbox " data-options="validType:'length[1,6]',prompt:'1~6位数字'" style="width: 173px; height: 30px;" />
    <span>客户姓名</span>
    <input id="CustomerName" class="easyui-textbox " data-options="validType:'length[1,50]',prompt:'1~50位汉字'" style="width: 173px; height: 30px;" />
    <span>客户级别</span>
    @*<select id="IsRetail" class="easyui-combobox" data-options="editable:false" style="width: 90px; height: 30px;">
            <option value="" selected="selected">全部</option>
            <option value="1">是</option>
            <option value="0">否</option>
        </select>*@
    <input name="SelectLevel" id="SelectLevel" class="easyui-combobox" style="width: 90px; height: 30px;" />
    <span>地区</span>
    <select id="Area" class="easyui-combobox" data-options="editable:false" style="width: 190px; height: 30px;">
    </select>
    <span>状态</span>
    <select id="IsValid" class="easyui-combobox" data-options="editable:false" style="width: 90px; height: 30px;">
        <option value="" selected="selected">全部</option>
        <option value="true">启用</option>
        <option value="false">停用</option>
    </select>
    <div>
        <a href="javascript:" class="easyui-linkbutton" iconcls="icon-add" plain="true" onclick="addCustomer()">新建</a>
        <a href="javascript:" class="easyui-linkbutton" iconcls="icon-edit" plain="true" onclick="editCustomer()">编辑</a>
        <a href="javascript:" class="easyui-linkbutton" iconcls="icon-no" plain="true" onclick="delCustomer()">删除</a>
        <a href="javascript:" class="easyui-linkbutton" plain="true" iconcls="icon-search" onclick="doSearch()">查询</a>
    </div>
</div>
<div id="dlg" class="easyui-dialog" title="新增" data-options="iconCls:'icon-save'"
     closed="true" style="width: 850px; height: 450px; padding: 10px 20px" buttons="#dlg-buttons" modal="true">
    <form id="fm" method="post" novalidate>
        <input type="hidden" name="Id" />
        @*<input type="hidden" name="BusinessCompanyType" id="BusinessCompanyType" value="" />*@
        <input type="hidden" id="CustomerLevelId" name="CustomerLevelId" />
        <input type="hidden" id="CustomerLevelName" name="CustomerLevelName" />
        <input type="hidden" id="switchbuttonIsValid" name="IsValid" />
        <input type="hidden" id="CustomerCode" name="CustomerCode" />
        <input type="hidden" id="Region" name="Region" />
        <div class="fitem">
            <label>
                客户名称:
            </label>
            <input name="CustomerName" class="easyui-textbox" data-options="required:true,validType:'length[1,50]',prompt:'1~50位汉字或字母'" style="width: 250px; height: 30px;" autocomplete="off" />
            <label>
                所属区域:
            </label>
            <input name="RegionId" id="RegionId" class="easyui-combobox" style="width: 250px; height: 30px;" />
        </div>
        <div class="fitem">
            <label>
                客户级别:
            </label>
            @*<select class="easyui-combogrid" style="width:250px; height: 30px;" name="LevelId" id="LevelId"></select>*@
            <input name="LevelId" id="LevelId" class="easyui-combobox" style="width: 250px; height: 30px;" />
            <label>
                余额:
            </label>
            <input name="Balance" id="Balance" class="easyui-numberbox" data-options="precision:'2'" style="width: 250px; height: 30px;" autocomplete="off" readonly />
        </div>
        <div class="fitem">
            <label>
                手机:
            </label>
            <input name="Mobile" class="easyui-numberbox" data-options="validType:'length[1,11]',prompt:'1~11位的数字'" style="width: 250px; height: 30px;" autocomplete="off" />
            <label>
                手机2:
            </label>
            <input name="Telephone" class="easyui-textbox" data-options="validType:'length[1,15]',prompt:'1~15位的字符'" style="width: 250px; height: 30px;" autocomplete="off" />
        </div>
        <div class="fitem">
            <label>
                QQ:
            </label>
            <input name="Qq" class="easyui-textbox" data-options="validType:'length[1,12]',prompt:'1~12位的字符'" style="width: 250px; height: 30px;" autocomplete="off" />
            <label>
                微信:
            </label>
            <input name="WeChat" class="easyui-textbox" data-options="validType:'length[1,100]',prompt:'1~50位的字符'" style="width: 250px; height: 30px;" autocomplete="off" />
        </div>
        <div class="fitem">
            <label>
                标签:
            </label>
            <input name="Tag" class="easyui-textbox" data-options="validType:'length[1,100]',prompt:'1~50位的字符'" style="width: 250px; height: 30px;" autocomplete="off" />
            <label>
                是否有效:
            </label>
            <input class="easyui-switchbutton" id="sbIsValid" data-options="onText:'是',offText:'否'">
        </div>
        <div class="fitem">
            @*<label>
                公司 :
            </label>
            <input class="easyui-combobox" id="CompanyId" name="CompanyId" style="width: 250px; height: 30px;" autocomplete="off" />*@
            <label>
                地址:
            </label>
            <input name="Address" class="easyui-textbox" data-options="validType:'length[1,100]',prompt:'1~100位的汉字'" style="width: 250px; height: 30px;" autocomplete="off" />
        </div>

        <div class="fitem">
            <label>
                备注:
            </label>
            <input name="Remark" class="easyui-textbox" data-options="validType:'length[1,100]'" style="width: 550px; height: 30px;" autocomplete="off" />
        </div>
    </form>
    @*<div id="companytoolbar">
        <input id="CompanyText" class="easyui-textbox " style="width: 173px; height: 30px;" />
        <a href="javascript:" class="easyui-linkbutton" plain="true" iconcls="icon-search" onclick="querycompany()">查询</a>
    </div>*@
</div>
<div id="dlg-buttons">
    <a href="javascript:void(0)" class="easyui-linkbutton" iconcls="icon-ok" onclick="saveCustomer()">保存</a>
    <a href="javascript:void(0)" class="easyui-linkbutton" iconcls="icon-cancel" onclick="javascript:$('#dlg').dialog('close')">取消</a>
</div>

<script src="~/Js/Global.js?v=20240704"></script>
<script language="javascript" type="text/javascript">
    $(function () {
        InitSelectCustomerLevel();
        InitSelectAreaInfoCombobox();
    });
    //function checkDecimal(newValue, oldValue) {
    //    if (newValue.toString().split('.').length > 1) {
    //        var decimalPart = newValue.toString().split('.')[1];
    //        if (decimalPart.length > 2) {
    //            alert(22);
    //            //$.messager.alert('Warning', '超过两位小数！');
    //            $(this).numberbox('setValue', oldValue);
    //        }
    //    }
    //}
    //function setFixRate() {
    //    //	var value = $('._fixRate').numberbox('getValue');//不能使用numberbox的getValue,这个是隐藏框的值
    //    //如果有小数点
    //    var value = $('#Balance').val();//这个才是输入框的值
    //    if (value.indexOf(".") >= 0) {//如果包含小数点
    //        if ((value.substring(value.indexOf(".") + 1).length) >= 2) {//如果小数位数已经有两位
    //            //			window.event.returnValue = false;
    //            //			$('._fixRate').val(value.replace(/^(\-)*(\d+)\.(\d\d).*$/,'$1$2.$3'));
    //            //			$('._fixRate').numberbox('filter',false);
    //            $('#Balance').numberbox('options').filter = function () {
    //                //让filter返回false会禁止按键输入
    //                return false;
    //            };
    //        } else {
    //            //记得写else,否则该nunberbox将不允许再次输入了
    //            $('#Balance').numberbox('options').filter = function () {
    //                return true;
    //            }
    //        }
    //    }
    //}

    $('#dg').datagrid({
        // 其他配置...
        onDblClickRow: function (rowIndex, rowData) {
            // 双击行时的操作
            //alert('双击行: ' + rowIndex + '，数据: ' + JSON.stringify(rowData));
            editCustomer();
        }
    });

    function InitAreaInfoCombobox() {
        $('#RegionId').combotree({
            url: "/BasicManage/GetAreaComboxTree",
            editable: false,
            valueField: 'Id',
            textField: 'AreaName',
            onSelect: function (rec) {
                $("#Region").val(rec.text);
            }
        })
    }

    function InitSelectAreaInfoCombobox() {
        $('#Area').combotree({
            url: "/BasicManage/GetAreaComboxTree",
            //editable: false,
            valueField: 'Id',
            textField: 'AreaName',
            onSelect: function (rec) {
            }
        })
    }

    function InitCustomerLevel() {
        $('#LevelId').combobox({
            url: "/CustomerLevel/GetCustomerLevel",
            valueField: 'Id',
            textField: 'LevelName',
            onSelect: function (rec) {
                $("#CustomerLevelId").val(rec.Id);
                $("#CustomerLevelName").val(rec.LevelName);
            }
        });
    }

    function InitSelectCustomerLevel() {
        var level_id_sign = 0;//标签
        $('#SelectLevel').combobox({
            url: "/CustomerLevel/GetCustomerLevel",
            valueField: 'Id',
            textField: 'LevelName',
            onLoadSuccess: function (data) {
                if (level_id_sign == 0) {
                    data.unshift({
                        'Id': '',
                        'LevelName': '全部',
                        'selected': 'true'
                    });
                    level_id_sign++;
                    $("#SelectLevel").combobox("loadData", data);
                }
            },
            onSelect: function (rec) {
                $("#CustomerLevelId").val(rec.Id);
                $("#CustomerLevelName").val(rec.LevelName);
            }
        });
    }

    function doSearch() {
        var area = $('#Area').combobox('getText');
        if (area == '顶级') {
            area = '';
        }
        $('#dg').datagrid('load', {
            CustomerName: $('#CustomerName').val(),
            CustomerLevelId: $('#SelectLevel').combobox('getValue'),
            CustomerCode: $('#CustomerCode').val(),
            IsValid: $('#IsValid').combobox('getValue'),
            Region: area
        });
    }

    function addCustomer() {
        InitAreaInfoCombobox();
        InitCustomerLevel();

        $('#fm').form('clear');
        $('#dlg').dialog('open');
        //InitCompanyById(CompanyId, companytoolbar)
        $('#sbIsValid').switchbutton({ checked: 'true' });
        url = '/Customer/AddCustomer';
    }

    function editCustomer() {
        var row = $('#dg').datagrid('getSelected');
        if (!row) {
            Helper.Show('请选择需要编辑的信息?');
            return;
        }
        $('#dlg').dialog('open').dialog('setTitle', '编辑');
        //InitCompanyById(CompanyId, companytoolbar)
        InitCustomerLevel();
        InitAreaInfoCombobox();

        $('#fm').form('load', row);
        if (row.CustomerLevelId == 0) {
            $('#LevelId').combobox('setValue', '');
        } else {
            $('#LevelId').combobox('setValue', row.CustomerLevelId);
        }

        $('#RegionId').combobox('setText', row.Region);

        $('#Region').val(row.Region);
        $('#sbIsValid').switchbutton({ checked: row.IsValid });
        $.messager.progress('close');

        //$.messager.progress({
        //    title: '请稍后',
        //    msg: '数据加载中...',
        //});
        //window.setTimeout(function () {
        //    $('#fm').form('load', row);
        //    //$('#AreaId').combobox('setValue', row.AreaId);
        //    if (row.CustomerLevelId == 0) {
        //        $('#LevelId').combobox('setValue', '');
        //    } else {
        //        $('#LevelId').combobox('setValue', row.CustomerLevelId);
        //    }

        //    $('#RegionId').combobox('setText', row.Region);
        //    //if (row.CompanyId != 0) {
        //    //    $('#CompanyId').combogrid('setValue', row.CompanyId);
        //    //} else {
        //    //    $('#CompanyId').combogrid('setValue', '');
        //    //}
        //    $('#Region').val(row.Region);
        //    //$('#LevelId').combogrid('setText', row.ClerkName);
        //    $('#sbIsValid').switchbutton({ checked: row.IsValid });
        //    $.messager.progress('close');
        //}, 1000);

        url = "/Customer/EditCustomer";
    }

    function delCustomer() {
        var row = $('#dg').datagrid('getSelected');
        if (!row) {
            Helper.Show('请选择需要删除的信息?');
            return false;
        }
        $.messager.confirm('提示信息', '您确认要删除吗?', function (r) {
            if (!r) { return false; }
            $.post("/Customer/DeleteCustomer", { id: row.Id }, function (json) {
                Helper.Show(json.Msg);
                if (json.Success) {
                    doSearch();
                }
            });
        });
    }

    function saveCustomer() {
        //$("#BusinessCompanyType").val(2);
        $("#switchbuttonIsValid").val($('#sbIsValid').switchbutton('options').checked);
        $('#fm').form('submit', {
            url: url,
            onSubmit: function () {
                return $(this).form('validate');
            },
            success: function (json) {
                var json = eval('(' + json + ')');
                Helper.Show(json.Msg);
                if (json.Success) {
                    $('#fm').form('clear');
                    $('#dlg').dialog('close');
                    $('#dg').datagrid('reload');
                }
            }
        });
    }

    //function querycompany() {
    //    $("#CompanyId").combogrid("grid").datagrid("reload", { "Company": $("#CompanyText").val() });
    //}

    function formatter_IsValid(value, rowData, rowIndex) {
        return rowData.IsValid ? '启用' : '停用';
    }
</script>
